﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.Controls;
using YArchitech.Revit.RevitExtension;

namespace YArchitech.Revit
{
	public class YJKPipeConnectorHelper
	{
		public void changePipeElbow(Pipe pipe1)
		{
			string a = "";
			FamilySymbol familySymbolByName = YJKRevitTools.GetFamilySymbolByName("Fitting", "水管弯头", pipe1.Document, ref a);
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe1);
			if (a == "")
			{
				hypiping.ChangeFirstElbow(familySymbolByName);
			}
		}

		public bool twoConnectorIsAlign(Connector conn1, Connector conn2)
		{
			bool result = false;
			if (conn1.Owner.Id == conn2.Owner.Id && conn1.CoordinateSystem.BasisZ.Z == 0.0 && conn2.CoordinateSystem.BasisZ.Z == 0.0 && Math.Abs(conn1.CoordinateSystem.BasisZ.X + conn2.CoordinateSystem.BasisZ.X) < 1E-05 && Math.Abs(conn1.CoordinateSystem.BasisZ.Y + conn2.CoordinateSystem.BasisZ.Y) < 1E-05)
			{
				result = true;
			}
			return result;
		}

		public void connect45Elbow(Pipe pipe1, XYZ xyz1, Pipe pipe2, XYZ xyz2, Autodesk.Revit.DB.Document doc)
		{
			if (new YJKRevitTools().TwoPipesOneisRightAndOneIsNot(pipe1, pipe2).Count < 2 && !YJKRevitTools.isRiserPipe(pipe1) && !YJKRevitTools.isRiserPipe(pipe2))
			{
				Line line = (pipe1.Location as LocationCurve).Curve as Line;
				Line line2 = (pipe2.Location as LocationCurve).Curve as Line;
				XYZ hyendPoint = line.GetEndPoint(0);
				XYZ hyendPoint2 = line.GetEndPoint(1);
				XYZ hyendPoint3 = line2.GetEndPoint(0);
				XYZ hyendPoint4 = line2.GetEndPoint(1);
				new YJKLine(line).IntersectionPoint(line2, false);
				double num = (pipe1.Diameter < pipe2.Diameter) ? pipe1.Diameter : pipe2.Diameter;
				Pipe sourcePipe = (pipe1.Diameter < pipe2.Diameter) ? pipe1 : pipe2;
				double num2 = YJKRevitTools.GetTwoXYVectorAngle(line.Direction, line2.Direction);
				this.changePipeElbow(pipe1);
				this.changePipeElbow(pipe2);
				XYZ xyz3 = (hyendPoint - hyendPoint2).Normalize();
				if (line2.Distance(hyendPoint2) < line2.Distance(hyendPoint))
				{
					xyz3 = (hyendPoint2 - hyendPoint).Normalize();
				}
				XYZ xyz4 = (hyendPoint3 - hyendPoint4).Normalize();
				if (line.Distance(hyendPoint4) < line.Distance(hyendPoint3))
				{
					xyz4 = (hyendPoint4 - hyendPoint3).Normalize();
				}
				num2 = xyz3.AngleTo(xyz4);
				double num3;
				if (Math.Abs(pipe1.Diameter - pipe2.Diameter) < 0.0001)
				{
					num3 = num * 3.0;
				}
				else
				{
					num3 = num * 3.0;
				}
				num3 /= Math.Sin(num2 / 2.0);
				XYZ xyz5 = YJKLine.get45NewXYZ(line, line2, num3, xyz1);
				XYZ xyz6 = YJKLine.get45NewXYZ(line2, line, num3, xyz2);
				Pipe pipe3 = YJKRevitTools.NewPipe(sourcePipe, xyz5, xyz6);
				pipe3.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(num);
				doc.Regenerate();
				if (pipe1.Diameter <= pipe2.Diameter)
				{
					this.creatELBOWACCORDTWOPIPE(pipe1, xyz1, pipe3, xyz5, doc, pipe2, xyz2);
					this.creatELBOWACCORDTWOPIPE(pipe2, xyz2, pipe3, xyz6, doc, pipe1, xyz1);
				}
				else
				{
					this.creatELBOWACCORDTWOPIPE(pipe2, xyz2, pipe3, xyz6, doc, pipe1, xyz1);
					this.creatELBOWACCORDTWOPIPE(pipe1, xyz1, pipe3, xyz5, doc, pipe2, xyz2);
				}
			}
			doc.Regenerate();
		}

		private XYZ getxyz(Pipe pipe)
		{
			Line curve = (pipe.Location as LocationCurve).Curve as Line;
			return (curve.GetEndPoint(0) + curve.GetEndPoint(1)) / 2.0;
		}

		public void pipeconnectpipe(Pipe pipe1, XYZ xyz1, Pipe pipe2, XYZ xyz2, Autodesk.Revit.DB.Document doc)
		{
			Line line = (pipe1.Location as LocationCurve).Curve as Line;
			Line line2 = (pipe2.Location as LocationCurve).Curve as Line;
			XYZ hyendPoint = line.GetEndPoint(0);
			line.GetEndPoint(1);
			XYZ hyendPoint2 = line2.GetEndPoint(0);
			XYZ hyendPoint3 = line2.GetEndPoint(1);
			new YJKLine(line);
			XYZ newLine = YJKLine.getNewLine(line, line2);
			double num = pipe2.get_Parameter(BuiltInParameter.RBS_PIPE_SLOPE).AsDouble();
			double num2 = hyendPoint2.DistanceTo(hyendPoint3);
			LocationCurve locationCurve = pipe2.Location as LocationCurve;
			if (num != 0.0)
			{
				double num3 = num2 * Math.Sin(num);
				XYZ endPoint = new XYZ(hyendPoint3.X, hyendPoint3.Y, hyendPoint2.Z + num3);
				Line curve = YJKLineEx.YJKGetBound(newLine, endPoint);
				locationCurve.Curve = curve;
			}
			(pipe1.Location as LocationCurve).Curve = YJKLineEx.YJKGetBound(hyendPoint, newLine);
			doc.Regenerate();
			this.changePipeElbow(pipe1);
			this.changePipeElbow(pipe2);
			Line line3 = (pipe1.Location as LocationCurve).Curve as Line;
			Line line4 = (pipe2.Location as LocationCurve).Curve as Line;
			YJKLine hyline = new YJKLine(line3);
			new YJKLine(line4);
			if (!hyline.IsLineOnLine(line4) && !hyline.IsParallel(line4) && hyline.IntersectionPoint(line4, 0.001) != null)
			{
				XYZ xyz3 = hyline.IntersectionPoint(line4, 0.001);
				double num4 = (xyz1 - xyz3).AngleTo(xyz2 - xyz3);
				if (num4 / Math.PI * 180.0 > 89.99 && num4 / Math.PI * 180.0 < 170.0)
				{
					this.creatELBOW(pipe1, pipe2, (pipe2.Location as LocationCurve).Curve.GetEndPoint(0), doc);
					doc.Regenerate();
				}
			}
		}

		public bool pipeconnectpipeForPolarAxis(Pipe pipe1, XYZ xyz1, Pipe pipe2, XYZ xyz2, Autodesk.Revit.DB.Document doc)
		{
			Line line = (pipe1.Location as LocationCurve).Curve as Line;
			Line line2 = (pipe2.Location as LocationCurve).Curve as Line;
			XYZ hyendPoint = line.GetEndPoint(0);
			line.GetEndPoint(1);
			XYZ hyendPoint2 = line2.GetEndPoint(0);
			XYZ hyendPoint3 = line2.GetEndPoint(1);
			new YJKLine(line);
			XYZ newLine = YJKLine.getNewLine(line, line2);
			double num = pipe2.get_Parameter(BuiltInParameter.RBS_PIPE_SLOPE).AsDouble();
			double num2 = hyendPoint2.DistanceTo(hyendPoint3);
			Location location = pipe2.Location;
			if (num != 0.0)
			{
				double num3 = num2 * Math.Sin(num);
				XYZ endPoint = new XYZ(hyendPoint3.X, hyendPoint3.Y, hyendPoint2.Z + num3);
				YJKLineEx.YJKGetBound(newLine, endPoint);
			}
			(pipe1.Location as LocationCurve).Curve = YJKLineEx.YJKGetBound(hyendPoint, newLine);
			doc.Regenerate();
			this.changePipeElbow(pipe1);
			this.changePipeElbow(pipe2);
			Line line3 = (pipe1.Location as LocationCurve).Curve as Line;
			Line line4 = (pipe2.Location as LocationCurve).Curve as Line;
			YJKLine hyline = new YJKLine(line3);
			new YJKLine(line4);
			if (!hyline.IsLineOnLine(line4) && !hyline.IsParallel(line4))
			{
				try
				{
					return this.creatELBOW(pipe1, pipe2, (pipe2.Location as LocationCurve).Curve.GetEndPoint(0), doc);
				}
				catch (Exception)
				{
				}
				return true;
			}
			return true;
		}

		public List<Pipe> TPipeConnectRPipe(Pipe riserPipe, Pipe pipe, FamilySymbol fs, int model, ref string errorMSG, bool isHengguan = false, double midPipeDiameter = 0.0)
		{
			Pipe pipe2;
			if (isHengguan)
			{
				pipe2 = pipe;
			}
			else
			{
				pipe2 = riserPipe;
			}
			double num;
			if (midPipeDiameter != 0.0)
			{
				num = midPipeDiameter / 304.8;
			}
			else
			{
				num = pipe2.get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble();
			}
			Document document = riserPipe.Document;
			bool flag = true;
			SubTransaction subTransaction = new SubTransaction(document);
			bool flag2 = false;
			List<Pipe> list = new List<Pipe>();
			Line line = (riserPipe.Location as LocationCurve).Curve as Line;
			Line line2 = (pipe.Location as LocationCurve).Curve as Line;
			XYZ hyendPoint = line.GetEndPoint(0);
			XYZ hyendPoint2 = line.GetEndPoint(1);
			XYZ hyendPoint3 = line2.GetEndPoint(0);
			XYZ hyendPoint4 = line2.GetEndPoint(1);
			double num2 = riserPipe.get_Parameter(BuiltInParameter.RBS_PIPE_OUTER_DIAMETER).AsDouble();
			double num3 = pipe.get_Parameter(BuiltInParameter.RBS_PIPE_OUTER_DIAMETER).AsDouble();
			double num4 = 19.685039370078741;
			XYZ dlineP = YJKRevitTools.GetDLineP(hyendPoint, hyendPoint2, hyendPoint3, hyendPoint4);
			double num5 = dlineP.DistanceTo(new XYZ(hyendPoint.X, hyendPoint.Y, dlineP.Z));
			XYZ dlineP2 = YJKRevitTools.GetDLineP(hyendPoint3, hyendPoint4, hyendPoint, hyendPoint2);
			double num6 = dlineP2.DistanceTo(new XYZ(dlineP.X, dlineP.Y, dlineP2.Z));
			if (model == 1)
			{
				if (num5 <= num4)
				{
					Location location = pipe.Location as LocationCurve;
					XYZ xyz = new XYZ(hyendPoint.X, hyendPoint.Y, dlineP.Z) - dlineP;
					location.Move(num5 * xyz.Normalize());
					flag2 = true;
				}
			}
			else if (model == 2)
			{
				if (num6 <= num4)
				{
					Location location2 = riserPipe.Location as LocationCurve;
					XYZ xyz2 = new XYZ(dlineP.X, dlineP.Y, dlineP2.Z) - dlineP2;
					location2.Move(num6 * xyz2.Normalize());
					flag2 = true;
				}
			}
			else if (model == 3)
			{
				if (num2 > num3)
				{
					if (num5 <= num4)
					{
						Location location3 = pipe.Location as LocationCurve;
						XYZ xyz3 = new XYZ(hyendPoint.X, hyendPoint.Y, dlineP.Z) - dlineP;
						location3.Move(num5 * xyz3.Normalize());
						flag2 = true;
					}
				}
				else if (num6 <= num4)
				{
					Location location4 = riserPipe.Location as LocationCurve;
					XYZ xyz4 = new XYZ(dlineP.X, dlineP.Y, dlineP2.Z) - dlineP2;
					location4.Move(num6 * xyz4.Normalize());
					flag2 = true;
				}
			}
			line = ((riserPipe.Location as LocationCurve).Curve as Line);
			line2 = ((pipe.Location as LocationCurve).Curve as Line);
			hyendPoint = line.GetEndPoint(0);
			hyendPoint2 = line.GetEndPoint(1);
			hyendPoint3 = line2.GetEndPoint(0);
			hyendPoint4 = line2.GetEndPoint(1);
			YJKLine hyline = new YJKLine(line2);
			new YJKLine(line);
			XYZ xyz5 = hyline.IntersectionPoint(line, 0.001);
			double num7 = pipe.get_Parameter(BuiltInParameter.RBS_PIPE_SLOPE).AsDouble();
			double num8;
			if (num2 > num3)
			{
				num8 = 2.26 * num2;
			}
			else if (num2 == num3)
			{
				num8 = 2.65 * num2;
			}
			else if (num3 > 1.5 * num2 && num3 < 2.0 * num2)
			{
				num8 = 2.25 * num2 + 0.5 * num3;
			}
			else if (num3 > 2.0 * num2 && num3 < 3.0 * num2)
			{
				num8 = 2.7 * num2 + 0.5 * num3;
			}
			else
			{
				num8 = 3.5 * num2 + 0.5 * num3;
			}
			List<FamilyInstance> list2 = new List<FamilyInstance>();
			try
			{
				subTransaction.Start();
				if (YJKLine.judgeTwoLineRelativePosition(line, line2, num2, num3) == 1)
				{
					if (YJKLine.judgeTwoLineTheirAreInTerst(line, num2, line2, num3) == 2)
					{
						if (flag2)
						{
							List<Pipe> list3 = new List<Pipe>();
							list3 = new YJKPiping<Pipe>(pipe).Separate(xyz5);
							FamilyInstance item;
							if (!flag)
							{
								item = document.Create.NewTeeFitting(this.getCloserConnector(list3[0], xyz5), this.getCloserConnector(list3[1], xyz5), this.getCloserConnector(riserPipe, xyz5));
							}
							else
							{
								item = this.NewTeeFittingY(this.newPipeListUseForTeeFitting(list3, riserPipe), fs, flag);
							}
							list.AddRange(list3);
							list2.Add(item);
						}
						else
						{
							errorMSG = "位置不对无法连接！";
						}
					}
					else if (YJKLine.judgeTwoLineTheirAreInTerst(line, num2, line2, num3) == 3 || YJKLine.judgeTwoLineTheirAreInTerst(line, num2, line2, num3) == 5 || YJKLine.judgeTwoLineTheirAreInTerst(line, num2, line2, num3) == 6)
					{
						if (flag2)
						{
							this.changePipeElbow(pipe);
							this.changePipeElbow(riserPipe);
							FamilyInstance item2 = document.Create.NewElbowFitting(this.getCloserConnector(pipe, xyz5), this.getCloserConnector(riserPipe, xyz5));
							list.Add(pipe);
							list2.Add(item2);
						}
						else
						{
							errorMSG = "位置不对无法连接！";
						}
					}
					else if (YJKLine.judgeTwoLineTheirAreInTerst(line, num2, line2, num3) == 4)
					{
						if (flag2)
						{
							List<Pipe> list4 = new List<Pipe>();
							list4 = new YJKPiping<Pipe>(riserPipe).Separate(xyz5);
							FamilyInstance item3;
							if (!flag)
							{
								item3 = document.Create.NewTeeFitting(this.getCloserConnector(list4[0], xyz5), this.getCloserConnector(list4[1], xyz5), this.getCloserConnector(pipe, xyz5));
							}
							else
							{
								item3 = this.NewTeeFittingY(this.newPipeListUseForTeeFitting(list4, pipe), fs, flag);
							}
							list.Add(pipe);
							list2.Add(item3);
						}
						else
						{
							errorMSG = "位置不对无法连接！";
						}
					}
				}
				else if (YJKLine.judgeTwoLineRelativePosition(line, line2, num2, num3) == 2)
				{
					bool flag3 = false;
					XYZ dlineP3 = YJKRevitTools.GetDLineP(hyendPoint3, hyendPoint4, hyendPoint, hyendPoint2);
					XYZ dlineP4 = YJKRevitTools.GetDLineP(hyendPoint, hyendPoint2, hyendPoint3, hyendPoint4);
					Pipe pipe3 = YJKRevitTools.NewPipe(pipe2, dlineP3, dlineP4);
					pipe3.get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(num);
					LocationCurve locationCurve = pipe3.Location as LocationCurve;
					XYZ hyendPoint5 = (pipe3.Location as LocationCurve).Curve.GetEndPoint(0);
					XYZ hyendPoint6 = (pipe3.Location as LocationCurve).Curve.GetEndPoint(1);
					double num9 = hyendPoint5.DistanceTo(hyendPoint6);
					if (num7 != 0.0 && !YJKRevitTools.isRiserPipe(pipe3))
					{
						double num10 = num9 * Math.Sin(num7);
						if (hyendPoint5.DistanceTo(dlineP3) < hyendPoint6.DistanceTo(dlineP3))
						{
							XYZ xyz6 = new XYZ(hyendPoint5.X, hyendPoint5.Y, hyendPoint5.Z - num10);
							YJKRevitTools.getNewPipe(pipe3, xyz6, document);
							Line curve = YJKLineEx.YJKGetBound(xyz6, hyendPoint6);
							locationCurve.Curve = curve;
						}
						else
						{
							Line curve2 = YJKLineEx.YJKGetBound(new XYZ(hyendPoint6.X, hyendPoint6.Y, hyendPoint6.Z - num10), hyendPoint5);
							locationCurve.Curve = curve2;
						}
						ElementTransformUtils.MoveElement(pipe3.Document, pipe3.Id, new XYZ(0.001, 0.001, 0.001));
						ElementTransformUtils.MoveElement(pipe3.Document, pipe3.Id, -1.0 * new XYZ(0.001, 0.001, 0.001));
					}
					if (num9 >= num8)
					{
						List<Pipe> list5 = new List<Pipe>();
						list5 = new YJKPiping<Pipe>(riserPipe).Separate(dlineP3);
						FamilyInstance item4;
						if (!flag)
						{
							item4 = document.Create.NewTeeFitting(this.getCloserConnector(list5[0], dlineP3), this.getCloserConnector(list5[1], dlineP3), this.getCloserConnector(pipe3, dlineP3));
						}
						else
						{
							item4 = this.NewTeeFittingY(this.newPipeListUseForTeeFitting(list5, pipe3), fs, flag);
						}
						this.changePipeElbow(pipe);
						this.changePipeElbow(pipe3);
						FamilyInstance item5 = document.Create.NewElbowFitting(this.getCloserConnector(pipe3, dlineP4), this.getCloserConnector(pipe, dlineP4));
						list.Add(pipe);
						list2.Add(item4);
						list2.Add(item5);
					}
					else if (!flag3)
					{
						document.DeleteElement(pipe3);
						document.Regenerate();
						errorMSG = "空间位置不足，无法创建过渡管道连接！";
					}
				}
				else if (YJKLine.judgeTwoLineRelativePosition(line, line2, num2, num3) == 3)
				{
					XYZ dlineP5 = YJKRevitTools.GetDLineP(hyendPoint3, hyendPoint4, hyendPoint, hyendPoint2);
					XYZ dlineP6 = YJKRevitTools.GetDLineP(hyendPoint, hyendPoint2, hyendPoint3, hyendPoint4);
					Pipe pipe4 = YJKRevitTools.NewPipe(pipe2, dlineP5, dlineP6);
					pipe4.get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(num);
					XYZ hyendPoint7 = (pipe4.Location as LocationCurve).Curve.GetEndPoint(0);
					XYZ hyendPoint8 = (pipe4.Location as LocationCurve).Curve.GetEndPoint(1);
					LocationCurve locationCurve2 = pipe4.Location as LocationCurve;
					double num11 = hyendPoint7.DistanceTo(hyendPoint8);
					bool flag4 = false;
					if (num7 != 0.0 && !YJKRevitTools.isRiserPipe(pipe4))
					{
						double num12 = num11 * Math.Sin(num7);
						if (hyendPoint7.DistanceTo(dlineP5) < hyendPoint8.DistanceTo(dlineP5))
						{
							Line curve3 = YJKLineEx.YJKGetBound(new XYZ(hyendPoint7.X, hyendPoint7.Y, hyendPoint7.Z + num12), hyendPoint8);
							locationCurve2.Curve = curve3;
						}
						else
						{
							Line curve4 = YJKLineEx.YJKGetBound(new XYZ(hyendPoint8.X, hyendPoint8.Y, hyendPoint8.Z + num12), hyendPoint7);
							locationCurve2.Curve = curve4;
						}
						ElementTransformUtils.MoveElement(pipe4.Document, pipe4.Id, new XYZ(0.001, 0.001, 0.001));
						ElementTransformUtils.MoveElement(pipe4.Document, pipe4.Id, -1.0 * new XYZ(0.001, 0.001, 0.001));
					}
					if (num11 >= num8)
					{
						this.changePipeElbow(pipe);
						this.changePipeElbow(pipe4);
						this.changePipeElbow(riserPipe);
						FamilyInstance item6 = document.Create.NewElbowFitting(this.getCloserConnector(pipe4, dlineP6), this.getCloserConnector(pipe, dlineP6));
						FamilyInstance item7 = document.Create.NewElbowFitting(this.getCloserConnector(pipe4, dlineP5), this.getCloserConnector(riserPipe, dlineP5));
						list.Add(pipe);
						list2.Add(item6);
						list2.Add(item7);
					}
					else if (!flag4)
					{
						document.DeleteElement(pipe4);
						document.Regenerate();
						errorMSG = "空间位置不足，无法创建过渡管道连接！";
					}
				}
				else if (YJKLine.judgeTwoLineRelativePosition(line, line2, num2, num3) == 4)
				{
					XYZ dlineP7 = YJKRevitTools.GetDLineP(hyendPoint3, hyendPoint4, hyendPoint, hyendPoint2);
					XYZ dlineP8 = YJKRevitTools.GetDLineP(hyendPoint, hyendPoint2, hyendPoint3, hyendPoint4);
					Pipe pipe5 = YJKRevitTools.NewPipe(pipe2, dlineP7, dlineP8);
					pipe5.get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(num);
					XYZ hyendPoint9 = (pipe5.Location as LocationCurve).Curve.GetEndPoint(0);
					XYZ hyendPoint10 = (pipe5.Location as LocationCurve).Curve.GetEndPoint(1);
					LocationCurve locationCurve3 = pipe5.Location as LocationCurve;
					double num13 = hyendPoint9.DistanceTo(hyendPoint10);
					bool flag5 = false;
					if (num7 != 0.0 && !YJKRevitTools.isRiserPipe(pipe5))
					{
						double num14 = num13 * Math.Sin(num7);
						if (hyendPoint9.DistanceTo(dlineP7) < hyendPoint10.DistanceTo(dlineP7))
						{
							Line curve5 = YJKLineEx.YJKGetBound(new XYZ(hyendPoint9.X, hyendPoint9.Y, hyendPoint9.Z + num14), hyendPoint10);
							locationCurve3.Curve = curve5;
						}
						else
						{
							Line curve6 = YJKLineEx.YJKGetBound(new XYZ(hyendPoint10.X, hyendPoint10.Y, hyendPoint10.Z + num14), hyendPoint9);
							locationCurve3.Curve = curve6;
						}
						ElementTransformUtils.MoveElement(pipe5.Document, pipe5.Id, new XYZ(0.001, 0.001, 0.001));
						ElementTransformUtils.MoveElement(pipe5.Document, pipe5.Id, -1.0 * new XYZ(0.001, 0.001, 0.001));
					}
					if (num13 >= num8)
					{
						List<Pipe> list6 = new List<Pipe>();
						list6 = new YJKPiping<Pipe>(pipe).Separate(dlineP8);
						FamilyInstance item8;
						if (!flag)
						{
							item8 = document.Create.NewTeeFitting(this.getCloserConnector(list6[0], dlineP8), this.getCloserConnector(list6[1], dlineP8), this.getCloserConnector(pipe5, dlineP8));
						}
						else
						{
							item8 = this.NewTeeFittingY(this.newPipeListUseForTeeFitting(list6, pipe5), fs, flag);
						}
						this.changePipeElbow(pipe5);
						this.changePipeElbow(riserPipe);
						FamilyInstance item9 = document.Create.NewElbowFitting(this.getCloserConnector(pipe5, dlineP7), this.getCloserConnector(riserPipe, dlineP7));
						list.AddRange(list6);
						list2.Add(item8);
						list2.Add(item9);
					}
					else if (!flag5)
					{
						document.DeleteElement(pipe5);
						document.Regenerate();
						errorMSG = "空间位置不足，无法创建过渡管道连接！";
					}
				}
				else if (YJKLine.judgeTwoLineRelativePosition(line, line2, num2, num3) == 5)
				{
					XYZ dlineP9 = YJKRevitTools.GetDLineP(hyendPoint3, hyendPoint4, hyendPoint, hyendPoint2);
					XYZ dlineP10 = YJKRevitTools.GetDLineP(hyendPoint, hyendPoint2, hyendPoint3, hyendPoint4);
					Pipe pipe6 = YJKRevitTools.NewPipe(pipe2, dlineP9, dlineP10);
					pipe6.get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(num);
					XYZ hyendPoint11 = (pipe6.Location as LocationCurve).Curve.GetEndPoint(0);
					XYZ hyendPoint12 = (pipe6.Location as LocationCurve).Curve.GetEndPoint(1);
					LocationCurve locationCurve4 = pipe6.Location as LocationCurve;
					double num15 = hyendPoint11.DistanceTo(hyendPoint12);
					bool flag6 = false;
					if (num7 != 0.0 && !YJKRevitTools.isRiserPipe(pipe6))
					{
						double num16 = num15 * Math.Sin(num7);
						if (hyendPoint11.DistanceTo(dlineP9) < hyendPoint12.DistanceTo(dlineP9))
						{
							Line curve7 = YJKLineEx.YJKGetBound(new XYZ(hyendPoint11.X, hyendPoint11.Y, hyendPoint11.Z - num16), hyendPoint12);
							locationCurve4.Curve = curve7;
						}
						else
						{
							Line curve8 = YJKLineEx.YJKGetBound(new XYZ(hyendPoint12.X, hyendPoint12.Y, hyendPoint12.Z - num16), hyendPoint11);
							locationCurve4.Curve = curve8;
						}
						ElementTransformUtils.MoveElement(pipe6.Document, pipe6.Id, new XYZ(0.001, 0.001, 0.001));
						ElementTransformUtils.MoveElement(pipe6.Document, pipe6.Id, -1.0 * new XYZ(0.001, 0.001, 0.001));
					}
					if (num15 >= num8)
					{
						List<Pipe> list7 = new List<Pipe>();
						list7 = new YJKPiping<Pipe>(pipe).Separate(dlineP10);
						FamilyInstance item10;
						if (!flag)
						{
							item10 = document.Create.NewTeeFitting(this.getCloserConnector(list7[0], dlineP10), this.getCloserConnector(list7[1], dlineP10), this.getCloserConnector(pipe6, dlineP10));
						}
						else
						{
							item10 = this.NewTeeFittingY(this.newPipeListUseForTeeFitting(list7, pipe6), fs, flag);
						}
						List<Pipe> list8 = new List<Pipe>();
						list8 = new YJKPiping<Pipe>(riserPipe).Separate(dlineP9);
						FamilyInstance item11;
						if (!flag)
						{
							item11 = document.Create.NewTeeFitting(this.getCloserConnector(list8[0], dlineP9), this.getCloserConnector(list8[1], dlineP9), this.getCloserConnector(pipe6, dlineP9));
						}
						else
						{
							item11 = this.NewTeeFittingY(this.newPipeListUseForTeeFitting(list8, pipe6), fs, flag);
						}
						list.AddRange(list7);
						list2.Add(item10);
						list2.Add(item11);
					}
					else if (!flag6)
					{
						document.DeleteElement(pipe6);
						document.Regenerate();
						errorMSG = "空间位置不足，无法创建过渡管道连接！";
					}
				}
				document.Regenerate();
				YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
				foreach (FamilyInstance fi in list2)
				{
					hyrotateFamilyInstance.MoveFamilyInstance(fi, new XYZ(0.001, 0.001, 0.001));
				}
				document.Regenerate();
				subTransaction.Commit();
			}
			catch
			{
				errorMSG = "连接失败！";
			}
			finally
			{
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return list;
		}

		public List<Pipe> TPipeConnectRPipe(Pipe riserPipe, Pipe pipe, Autodesk.Revit.DB.Document doc, FamilySymbol fs, bool isSSTee, int model, ref string errorMSG, bool isHengguan = false)
		{
			Pipe sourcePipe;
			if (isHengguan)
			{
				sourcePipe = pipe;
			}
			else
			{
				sourcePipe = riserPipe;
			}
			SubTransaction subTransaction = new SubTransaction(doc);
			bool flag = false;
			List<Pipe> list = new List<Pipe>();
			Line line = (riserPipe.Location as LocationCurve).Curve as Line;
			Line line2 = (pipe.Location as LocationCurve).Curve as Line;
			XYZ hyendPoint = line.GetEndPoint(0);
			XYZ hyendPoint2 = line.GetEndPoint(1);
			XYZ hyendPoint3 = line2.GetEndPoint(0);
			XYZ hyendPoint4 = line2.GetEndPoint(1);
			double num = riserPipe.get_Parameter(BuiltInParameter.RBS_PIPE_OUTER_DIAMETER).AsDouble();
			double num2 = pipe.get_Parameter(BuiltInParameter.RBS_PIPE_OUTER_DIAMETER).AsDouble();
			double num3 = (riserPipe.get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble() + pipe.get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble()) / 2.0;
			XYZ dlineP = YJKRevitTools.GetDLineP(hyendPoint, hyendPoint2, hyendPoint3, hyendPoint4);
			double num4 = dlineP.DistanceTo(new XYZ(hyendPoint.X, hyendPoint.Y, dlineP.Z));
			XYZ dlineP2 = YJKRevitTools.GetDLineP(hyendPoint3, hyendPoint4, hyendPoint, hyendPoint2);
			double num5 = dlineP2.DistanceTo(new XYZ(dlineP.X, dlineP.Y, dlineP2.Z));
			if (model == 1)
			{
				if (num4 <= num3)
				{
					Location location = pipe.Location as LocationCurve;
					XYZ xyz = new XYZ(hyendPoint.X, hyendPoint.Y, dlineP.Z) - dlineP;
					location.Move(num4 * xyz.Normalize());
					flag = true;
				}
			}
			else if (model == 2)
			{
				if (num5 <= num3)
				{
					Location location2 = riserPipe.Location as LocationCurve;
					XYZ xyz2 = new XYZ(dlineP.X, dlineP.Y, dlineP2.Z) - dlineP2;
					location2.Move(num5 * xyz2.Normalize());
					flag = true;
				}
			}
			else if (model == 3)
			{
				if (num > num2)
				{
					if (num4 <= num3)
					{
						Location location3 = pipe.Location as LocationCurve;
						XYZ xyz3 = new XYZ(hyendPoint.X, hyendPoint.Y, dlineP.Z) - dlineP;
						location3.Move(num4 * xyz3.Normalize());
						flag = true;
					}
				}
				else if (num5 <= num3)
				{
					Location location4 = riserPipe.Location as LocationCurve;
					XYZ xyz4 = new XYZ(dlineP.X, dlineP.Y, dlineP2.Z) - dlineP2;
					location4.Move(num5 * xyz4.Normalize());
					flag = true;
				}
			}
			else if (model != 1 && model != 2 && model != 3 && (num4 < 0.016404199475065617 || num5 < 0.016404199475065617))
			{
				Location location5 = riserPipe.Location as LocationCurve;
				XYZ xyz5 = new XYZ(dlineP.X, dlineP.Y, dlineP2.Z) - dlineP2;
				location5.Move(num5 * xyz5.Normalize());
				flag = true;
			}
			line = ((riserPipe.Location as LocationCurve).Curve as Line);
			line2 = ((pipe.Location as LocationCurve).Curve as Line);
			hyendPoint = line.GetEndPoint(0);
			hyendPoint2 = line.GetEndPoint(1);
			hyendPoint3 = line2.GetEndPoint(0);
			hyendPoint4 = line2.GetEndPoint(1);
			YJKLine hyline = new YJKLine(line2);
			new YJKLine(line);
			XYZ xyz6 = hyline.IntersectionPoint(line, 0.001);
			double num6 = pipe.get_Parameter(BuiltInParameter.RBS_PIPE_SLOPE).AsDouble();
			double num7;
			if (num > num2)
			{
				num7 = 2.26 * num;
			}
			else if (num == num2)
			{
				num7 = 2.65 * num;
			}
			else if (num2 > 1.5 * num && num2 < 2.0 * num)
			{
				num7 = 2.25 * num + 0.5 * num2;
			}
			else if (num2 > 2.0 * num && num2 < 3.0 * num)
			{
				num7 = 2.7 * num + 0.5 * num2;
			}
			else
			{
				num7 = 3.5 * num + 0.5 * num2;
			}
			List<FamilyInstance> list2 = new List<FamilyInstance>();
			try
			{
				subTransaction.Start();
				if (YJKLine.judgeTwoLineRelativePosition(line, line2, num, num2) == 1)
				{
					if (YJKLine.judgeTwoLineTheirAreInTerst(line, num, line2, num2) == 2)
					{
						if (flag)
						{
							List<Pipe> list3 = new List<Pipe>();
							list3 = new YJKPiping<Pipe>(pipe).Separate(xyz6);
							FamilyInstance item;
							if (!isSSTee)
							{
								if (errorMSG == "temp")
								{
									List<Pipe> list4 = new List<Pipe>();
									list4 = new YJKPiping<Pipe>(riserPipe).Separate(xyz6);
									item = doc.Create.NewCrossFitting(this.getCloserConnector(list3[0], xyz6), this.getCloserConnector(list3[1], xyz6), this.getCloserConnector(list4[0], xyz6), this.getCloserConnector(list4[1], xyz6));
									errorMSG = "";
								}
								else
								{
									item = doc.Create.NewTeeFitting(this.getCloserConnector(list3[0], xyz6), this.getCloserConnector(list3[1], xyz6), this.getCloserConnector(riserPipe, xyz6));
								}
							}
							else
							{
								item = this.NewTeeFittingY(this.newPipeListUseForTeeFitting(list3, riserPipe), fs, isSSTee);
							}
							list.AddRange(list3);
							list2.Add(item);
						}
						else
						{
							errorMSG = "位置不对无法连接！";
						}
					}
					else if (YJKLine.judgeTwoLineTheirAreInTerst(line, num, line2, num2) == 3 || YJKLine.judgeTwoLineTheirAreInTerst(line, num, line2, num2) == 5 || YJKLine.judgeTwoLineTheirAreInTerst(line, num, line2, num2) == 6)
					{
						if (flag)
						{
							this.changePipeElbow(pipe);
							this.changePipeElbow(riserPipe);
							FamilyInstance item2 = doc.Create.NewElbowFitting(this.getCloserConnector(pipe, xyz6), this.getCloserConnector(riserPipe, xyz6));
							list.Add(pipe);
							list2.Add(item2);
						}
						else
						{
							errorMSG = "位置不对无法连接！";
						}
					}
					else if (YJKLine.judgeTwoLineTheirAreInTerst(line, num, line2, num2) == 4)
					{
						if (flag)
						{
							List<Pipe> list5 = new List<Pipe>();
							list5 = new YJKPiping<Pipe>(riserPipe).Separate(xyz6);
							FamilyInstance item3;
							if (!isSSTee)
							{
								item3 = doc.Create.NewTeeFitting(this.getCloserConnector(list5[0], xyz6), this.getCloserConnector(list5[1], xyz6), this.getCloserConnector(pipe, xyz6));
							}
							else
							{
								item3 = this.NewTeeFittingY(this.newPipeListUseForTeeFitting(list5, pipe), fs, isSSTee);
							}
							list.Add(pipe);
							list2.Add(item3);
						}
						else
						{
							errorMSG = "位置不对无法连接！";
						}
					}
				}
				else if (YJKLine.judgeTwoLineRelativePosition(line, line2, num, num2) == 2)
				{
					bool flag2 = false;
					XYZ dlineP3 = YJKRevitTools.GetDLineP(hyendPoint3, hyendPoint4, hyendPoint, hyendPoint2);
					XYZ dlineP4 = YJKRevitTools.GetDLineP(hyendPoint, hyendPoint2, hyendPoint3, hyendPoint4);
					Pipe pipe2 = YJKRevitTools.NewPipe(sourcePipe, dlineP3, dlineP4);
					LocationCurve locationCurve = pipe2.Location as LocationCurve;
					XYZ hyendPoint5 = (pipe2.Location as LocationCurve).Curve.GetEndPoint(0);
					XYZ hyendPoint6 = (pipe2.Location as LocationCurve).Curve.GetEndPoint(1);
					double num8 = hyendPoint5.DistanceTo(hyendPoint6);
					if (num6 != 0.0 && !YJKRevitTools.isRiserPipe(pipe2))
					{
						double num9 = num8 * Math.Sin(num6);
						if (hyendPoint5.DistanceTo(dlineP3) < hyendPoint6.DistanceTo(dlineP3))
						{
							XYZ xyz7 = new XYZ(hyendPoint5.X, hyendPoint5.Y, hyendPoint5.Z - num9);
							YJKRevitTools.getNewPipe(pipe2, xyz7, doc);
							Line curve = YJKLineEx.YJKGetBound(xyz7, hyendPoint6);
							locationCurve.Curve = curve;
						}
						else
						{
							Line curve2 = YJKLineEx.YJKGetBound(new XYZ(hyendPoint6.X, hyendPoint6.Y, hyendPoint6.Z - num9), hyendPoint5);
							locationCurve.Curve = curve2;
						}
						ElementTransformUtils.MoveElement(pipe2.Document, pipe2.Id, new XYZ(0.001, 0.001, 0.001));
						ElementTransformUtils.MoveElement(pipe2.Document, pipe2.Id, -1.0 * new XYZ(0.001, 0.001, 0.001));
					}
					if (num8 >= num7)
					{
						List<Pipe> list6 = new List<Pipe>();
						list6 = new YJKPiping<Pipe>(riserPipe).Separate(dlineP3);
						FamilyInstance item4;
						if (!isSSTee)
						{
							item4 = doc.Create.NewTeeFitting(this.getCloserConnector(list6[0], dlineP3), this.getCloserConnector(list6[1], dlineP3), this.getCloserConnector(pipe2, dlineP3));
						}
						else
						{
							item4 = this.NewTeeFittingY(this.newPipeListUseForTeeFitting(list6, pipe2), fs, isSSTee);
						}
						this.changePipeElbow(pipe);
						this.changePipeElbow(pipe2);
						FamilyInstance item5 = doc.Create.NewElbowFitting(this.getCloserConnector(pipe2, dlineP4), this.getCloserConnector(pipe, dlineP4));
						list.Add(pipe);
						list2.Add(item4);
						list2.Add(item5);
					}
					else if (!flag2)
					{
						doc.DeleteElement(pipe2);
						doc.Regenerate();
						errorMSG = "空间位置不足，无法创建过渡管道连接！";
					}
				}
				else if (YJKLine.judgeTwoLineRelativePosition(line, line2, num, num2) == 3)
				{
					XYZ dlineP5 = YJKRevitTools.GetDLineP(hyendPoint3, hyendPoint4, hyendPoint, hyendPoint2);
					XYZ dlineP6 = YJKRevitTools.GetDLineP(hyendPoint, hyendPoint2, hyendPoint3, hyendPoint4);
					Pipe pipe3 = YJKRevitTools.NewPipe(sourcePipe, dlineP5, dlineP6);
					XYZ hyendPoint7 = (pipe3.Location as LocationCurve).Curve.GetEndPoint(0);
					XYZ hyendPoint8 = (pipe3.Location as LocationCurve).Curve.GetEndPoint(1);
					LocationCurve locationCurve2 = pipe3.Location as LocationCurve;
					double num10 = hyendPoint7.DistanceTo(hyendPoint8);
					bool flag3 = false;
					if (num6 != 0.0 && !YJKRevitTools.isRiserPipe(pipe3))
					{
						double num11 = num10 * Math.Sin(num6);
						if (hyendPoint7.DistanceTo(dlineP5) < hyendPoint8.DistanceTo(dlineP5))
						{
							Line curve3 = YJKLineEx.YJKGetBound(new XYZ(hyendPoint7.X, hyendPoint7.Y, hyendPoint7.Z + num11), hyendPoint8);
							locationCurve2.Curve = curve3;
						}
						else
						{
							Line curve4 = YJKLineEx.YJKGetBound(new XYZ(hyendPoint8.X, hyendPoint8.Y, hyendPoint8.Z + num11), hyendPoint7);
							locationCurve2.Curve = curve4;
						}
						ElementTransformUtils.MoveElement(pipe3.Document, pipe3.Id, new XYZ(0.001, 0.001, 0.001));
						ElementTransformUtils.MoveElement(pipe3.Document, pipe3.Id, -1.0 * new XYZ(0.001, 0.001, 0.001));
					}
					if (num10 >= num7)
					{
						this.changePipeElbow(pipe);
						this.changePipeElbow(pipe3);
						this.changePipeElbow(riserPipe);
						FamilyInstance item6 = doc.Create.NewElbowFitting(this.getCloserConnector(pipe3, dlineP6), this.getCloserConnector(pipe, dlineP6));
						FamilyInstance item7 = doc.Create.NewElbowFitting(this.getCloserConnector(pipe3, dlineP5), this.getCloserConnector(riserPipe, dlineP5));
						list.Add(pipe);
						list2.Add(item6);
						list2.Add(item7);
					}
					else if (!flag3)
					{
						doc.DeleteElement(pipe3);
						doc.Regenerate();
						errorMSG = "空间位置不足，无法创建过渡管道连接！";
					}
				}
				else if (YJKLine.judgeTwoLineRelativePosition(line, line2, num, num2) == 4)
				{
					XYZ dlineP7 = YJKRevitTools.GetDLineP(hyendPoint3, hyendPoint4, hyendPoint, hyendPoint2);
					XYZ dlineP8 = YJKRevitTools.GetDLineP(hyendPoint, hyendPoint2, hyendPoint3, hyendPoint4);
					Pipe pipe4 = YJKRevitTools.NewPipe(sourcePipe, dlineP7, dlineP8);
					XYZ hyendPoint9 = (pipe4.Location as LocationCurve).Curve.GetEndPoint(0);
					XYZ hyendPoint10 = (pipe4.Location as LocationCurve).Curve.GetEndPoint(1);
					LocationCurve locationCurve3 = pipe4.Location as LocationCurve;
					double num12 = hyendPoint9.DistanceTo(hyendPoint10);
					bool flag4 = false;
					if (num6 != 0.0 && !YJKRevitTools.isRiserPipe(pipe4))
					{
						double num13 = num12 * Math.Sin(num6);
						if (hyendPoint9.DistanceTo(dlineP7) < hyendPoint10.DistanceTo(dlineP7))
						{
							Line curve5 = YJKLineEx.YJKGetBound(new XYZ(hyendPoint9.X, hyendPoint9.Y, hyendPoint9.Z + num13), hyendPoint10);
							locationCurve3.Curve = curve5;
						}
						else
						{
							Line curve6 = YJKLineEx.YJKGetBound(new XYZ(hyendPoint10.X, hyendPoint10.Y, hyendPoint10.Z + num13), hyendPoint9);
							locationCurve3.Curve = curve6;
						}
						ElementTransformUtils.MoveElement(pipe4.Document, pipe4.Id, new XYZ(0.001, 0.001, 0.001));
						ElementTransformUtils.MoveElement(pipe4.Document, pipe4.Id, -1.0 * new XYZ(0.001, 0.001, 0.001));
					}
					if (num12 >= num7)
					{
						List<Pipe> list7 = new List<Pipe>();
						list7 = new YJKPiping<Pipe>(pipe).Separate(dlineP8);
						FamilyInstance item8;
						if (!isSSTee)
						{
							item8 = doc.Create.NewTeeFitting(this.getCloserConnector(list7[0], dlineP8), this.getCloserConnector(list7[1], dlineP8), this.getCloserConnector(pipe4, dlineP8));
						}
						else
						{
							item8 = this.NewTeeFittingY(this.newPipeListUseForTeeFitting(list7, pipe4), fs, isSSTee);
						}
						this.changePipeElbow(pipe4);
						this.changePipeElbow(riserPipe);
						FamilyInstance item9 = doc.Create.NewElbowFitting(this.getCloserConnector(pipe4, dlineP7), this.getCloserConnector(riserPipe, dlineP7));
						list.AddRange(list7);
						list2.Add(item8);
						list2.Add(item9);
					}
					else if (!flag4)
					{
						doc.DeleteElement(pipe4);
						doc.Regenerate();
						errorMSG = "空间位置不足，无法创建过渡管道连接！";
					}
				}
				else if (YJKLine.judgeTwoLineRelativePosition(line, line2, num, num2) == 5)
				{
					XYZ dlineP9 = YJKRevitTools.GetDLineP(hyendPoint3, hyendPoint4, hyendPoint, hyendPoint2);
					XYZ dlineP10 = YJKRevitTools.GetDLineP(hyendPoint, hyendPoint2, hyendPoint3, hyendPoint4);
					Pipe pipe5 = YJKRevitTools.NewPipe(sourcePipe, dlineP9, dlineP10);
					XYZ hyendPoint11 = (pipe5.Location as LocationCurve).Curve.GetEndPoint(0);
					XYZ hyendPoint12 = (pipe5.Location as LocationCurve).Curve.GetEndPoint(1);
					LocationCurve locationCurve4 = pipe5.Location as LocationCurve;
					double num14 = hyendPoint11.DistanceTo(hyendPoint12);
					bool flag5 = false;
					if (num6 != 0.0 && !YJKRevitTools.isRiserPipe(pipe5))
					{
						double num15 = num14 * Math.Sin(num6);
						if (hyendPoint11.DistanceTo(dlineP9) < hyendPoint12.DistanceTo(dlineP9))
						{
							Line curve7 = YJKLineEx.YJKGetBound(new XYZ(hyendPoint11.X, hyendPoint11.Y, hyendPoint11.Z - num15), hyendPoint12);
							locationCurve4.Curve = curve7;
						}
						else
						{
							Line curve8 = YJKLineEx.YJKGetBound(new XYZ(hyendPoint12.X, hyendPoint12.Y, hyendPoint12.Z - num15), hyendPoint11);
							locationCurve4.Curve = curve8;
						}
						ElementTransformUtils.MoveElement(pipe5.Document, pipe5.Id, new XYZ(0.001, 0.001, 0.001));
						ElementTransformUtils.MoveElement(pipe5.Document, pipe5.Id, -1.0 * new XYZ(0.001, 0.001, 0.001));
					}
					if (num14 >= num7)
					{
						List<Pipe> list8 = new List<Pipe>();
						list8 = new YJKPiping<Pipe>(pipe).Separate(dlineP10);
						FamilyInstance item10;
						if (!isSSTee)
						{
							item10 = doc.Create.NewTeeFitting(this.getCloserConnector(list8[0], dlineP10), this.getCloserConnector(list8[1], dlineP10), this.getCloserConnector(pipe5, dlineP10));
						}
						else
						{
							item10 = this.NewTeeFittingY(this.newPipeListUseForTeeFitting(list8, pipe5), fs, isSSTee);
						}
						List<Pipe> list9 = new List<Pipe>();
						list9 = new YJKPiping<Pipe>(riserPipe).Separate(dlineP9);
						FamilyInstance item11;
						if (!isSSTee)
						{
							item11 = doc.Create.NewTeeFitting(this.getCloserConnector(list9[0], dlineP9), this.getCloserConnector(list9[1], dlineP9), this.getCloserConnector(pipe5, dlineP9));
						}
						else
						{
							item11 = this.NewTeeFittingY(this.newPipeListUseForTeeFitting(list9, pipe5), fs, isSSTee);
						}
						list.AddRange(list8);
						list2.Add(item10);
						list2.Add(item11);
					}
					else if (!flag5)
					{
						doc.DeleteElement(pipe5);
						doc.Regenerate();
						errorMSG = "空间位置不足，无法创建过渡管道连接！";
					}
				}
				doc.Regenerate();
				using (List<Pipe>.Enumerator enumerator = list.GetEnumerator())
				{
					if (enumerator.MoveNext())
					{
						Pipe pipe6 = enumerator.Current;
						ElementTransformUtils.MoveElement(pipe6.Document, pipe6.Id, new XYZ(0.001, 0.001, 0.001));
						ElementTransformUtils.MoveElement(pipe6.Document, pipe6.Id, -1.0 * new XYZ(0.001, 0.001, 0.001));
					}
				}
				subTransaction.Commit();
			}
			catch
			{
				errorMSG = "连接失败！";
			}
			finally
			{
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return list;
		}

		private void deleteConnectFitting(Connector conn)
		{
			Document document = conn.Owner.Document;
			if (conn.IsConnected)
			{
				Element owner = YJKRevitTools.GetConnectConnector(conn).Owner;
				if (owner.Category.Id.IntegerValue == -2008049)
				{
					document.DeleteElement(owner);
					document.Regenerate();
				}
			}
		}

		private void creatELBOWACCORDTWOPIPE(Pipe pipe1, XYZ point1, Pipe pipe2, XYZ newPipeXYZ, Autodesk.Revit.DB.Document doc, Pipe pipe3, XYZ xyz3)
		{
			YJKLine hyline = new YJKLine((pipe1.Location as LocationCurve).Curve as Line);
			YJKLine hyline2 = new YJKLine((pipe3.Location as LocationCurve).Curve as Line);
			XYZ start = new XYZ(hyline.StartPoint.X, hyline.StartPoint.Y, 0.0);
			XYZ end = new XYZ(hyline.EndPoint.X, hyline.EndPoint.Y, 0.0);
			XYZ start2 = new XYZ(hyline2.StartPoint.X, hyline2.StartPoint.Y, 0.0);
			XYZ end2 = new XYZ(hyline2.EndPoint.X, hyline2.EndPoint.Y, 0.0);
			YJKLine hyline3 = new YJKLine(start, end);
			YJKLine hyline4 = new YJKLine(start2, end2);
			XYZ point2 = hyline3.IntersectionPoint(hyline4.Line, false);
			XYZ project = hyline.GetProject(point2);
			Connector appropriateConnector = new YJKPiping<Pipe>(pipe1, point1).GetAppropriateConnector(project);
			Connector closeConnector = new YJKPiping<Pipe>(pipe2).GetCloseConnector(newPipeXYZ, false);
			this.changePipeElbow(pipe1);
			this.changePipeElbow(pipe2);
			if (appropriateConnector.IsConnected)
			{
				Element owner = YJKRevitTools.GetConnectConnector(appropriateConnector).Owner;
				if (owner.Category.Id.IntegerValue == -2008049)
				{
					doc.DeleteElement(owner);
					doc.Regenerate();
				}
			}
			if (closeConnector.IsConnected)
			{
				Element owner2 = YJKRevitTools.GetConnectConnector(closeConnector).Owner;
				if (owner2.Category.Id.IntegerValue == -2008049)
				{
					doc.DeleteElement(owner2);
					doc.Regenerate();
				}
			}
			if (!appropriateConnector.IsConnected && !closeConnector.IsConnected)
			{
				doc.Create.NewElbowFitting(appropriateConnector, closeConnector);
			}
		}

		public bool creatELBOW(Pipe pipe1, Pipe pipe2, XYZ newPipeXYZ, Autodesk.Revit.DB.Document doc)
		{
			this.changePipeElbow(pipe1);
			this.changePipeElbow(pipe2);
			Connector closeConnector = new YJKPiping<Pipe>(pipe1).GetCloseConnector(newPipeXYZ, false);
			Connector closeConnector2 = new YJKPiping<Pipe>(pipe2).GetCloseConnector(newPipeXYZ, false);
			if (!(doc.GetElementById(pipe1.PipeType.RoutingPreferenceManager.GetRule((Autodesk.Revit.DB.RoutingPreferenceRuleGroupType)1, 0).MEPPartId) is FamilySymbol))
			{
				return false;
			}
			doc.Create.NewElbowFitting(closeConnector, closeConnector2);
			return true;
		}

		private Connector getCloserConnector(Pipe pipe, XYZ xyz)
		{
			return new YJKPiping<Pipe>(pipe).GetCloseConnector(xyz, false);
		}

		private List<Pipe> newPipeListUseForTeeFitting(List<Pipe> listfirst, Pipe pipe)
		{
			List<Pipe> list = new List<Pipe>();
			list.AddRange(YJKRevitTools.judgeWhitchPipeIsHigh(listfirst));
			list.Add(pipe);
			return list;
		}

		private double GetAngle(XYZ baseVector, XYZ teeVector, XYZ pipeVector)
		{
			double twoVectorAngle = YJKRevitTools.GetTwoVectorAngle(baseVector, teeVector, baseVector.CrossProduct(teeVector));
			XYZ xyz = 1.0 / Math.Cos(twoVectorAngle) * teeVector - baseVector;
			YJKRevitTools.GetTwoVectorAngle(baseVector, pipeVector, baseVector.CrossProduct(pipeVector));
			XYZ xyz2 = 1.0 / Math.Cos(twoVectorAngle) * pipeVector - baseVector;
			return YJKRevitTools.GetTwoVectorAngle(xyz, xyz2, xyz.CrossProduct(xyz2));
		}

		private bool IsSameConnector(Connector conn1, Connector conn2, double deviation)
		{
			bool result = true;
			if (conn1 == null || conn2 == null)
			{
				return false;
			}
			double num = conn1.CoordinateSystem.BasisZ.AngleTo(-conn2.CoordinateSystem.BasisZ);
			if (!conn1.CoordinateSystem.BasisZ.IsAlmostEqualTo(-conn2.CoordinateSystem.BasisZ) || num > deviation)
			{
				result = false;
			}
			return result;
		}

		public FamilyInstance NewTeeFittingY(IList<Pipe> pipes, FamilySymbol fs, bool IsSailingTee)
		{
			SubTransaction subTransaction = null;
			FamilyInstance familyInstance = null;
			Document document = null;
			FamilyInstance result;
			try
			{
				if (pipes.Count < 3)
				{
					throw new System.ArgumentNullException("Pipe", "is null");
				}
				document = pipes[0].Document;
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipes[0]);
				YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipes[1]);
				YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(pipes[2]);
				YJKLine hyline = new YJKLine((pipes[0].Location as LocationCurve).Curve as Line);
				YJKLine hyline2 = new YJKLine((pipes[2].Location as LocationCurve).Curve as Line);
				if (IsSailingTee)
				{
					hypiping = new YJKPiping<Pipe>(pipes[1]);
					hypiping2 = new YJKPiping<Pipe>(pipes[0]);
					hyline = new YJKLine((pipes[1].Location as LocationCurve).Curve as Line);
				}
				ConnectHelper connectHelper = new ConnectHelper();
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				XYZ xyz = hyline.IntersectionPoint(hyline2.Line, false);
				if (xyz == null)
				{
					throw new Exception("请检查水管位置是否正确");
				}
				YJKPiping<Pipe> hypiping4;
				if (hypiping.Piping.UniqueId == hypiping2.Piping.UniqueId)
				{
					new List<Pipe>();
					hypiping4 = new YJKPiping<Pipe>(((IList<Pipe>)hypiping.Separate(xyz))[1], hypiping2.GlobalPoint);
					document.Regenerate();
				}
				else
				{
					hypiping4 = new YJKPiping<Pipe>(hypiping2.Piping, hypiping2.GlobalPoint);
				}
				Level level;
				if (document.ActiveView.GenLevel != null)
				{
					level = document.ActiveView.GenLevel;
				}
				else
				{
					level = YJKRevitTools.GetLevelClose(xyz, document);
				}
				UnifiedModified.ActivateFamilySymbol(fs);
				FamilyInstance familyInstance2 = document.Create.NewFamilyInstance(xyz, fs, level, (Autodesk.Revit.DB.Structure.StructuralType)0);
				document.Regenerate();
				if ((familyInstance2.Location as LocationPoint).Point.DistanceTo(xyz) > 0.001)
				{
					XYZ xyz2 = xyz - (familyInstance2.Location as LocationPoint).Point;
					ElementTransformUtils.MoveElement(document, familyInstance2.Id, xyz2);
				}
				if (familyInstance2.GetParameter("入口直径") == null)
				{
					subTransaction.RollBack();
					subTransaction = null;
				}
				Parameter parameter = hypiping.Piping.get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM);
				if (parameter == null)
				{
					throw new Exception("水管直径未找到");
				}
				if (!parameter.IsReadOnly)
				{
					familyInstance2.GetParameter("入口直径").Set(parameter.AsDouble());
				}
				this.SettingTeeParameter(familyInstance2, hypiping, hypiping3, 2);
				this.SettingTeeParameter(familyInstance2, hypiping, hypiping4, 1);
				document.Regenerate();
				YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
				XYZ point = (familyInstance2.Location as LocationPoint).Point;
				Connector closeConnector = hypiping.GetCloseConnector(point, false);
				Connector distanceConnector = hypiping.GetDistanceConnector(point);
				Connector closeConnector2 = hypiping4.GetCloseConnector(point, false);
				Connector distanceConnector2 = hypiping4.GetDistanceConnector(point);
				Connector connector = connectHelper.GetTeeMainConnector(familyInstance2);
				Connector teeOutConnector = connectHelper.GetTeeOutConnector(familyInstance2, connector.CoordinateSystem.BasisZ);
				Connector connector2 = connectHelper.GetTeeSideConnector(familyInstance2, connector.CoordinateSystem.BasisZ);
				if (connector != null && closeConnector != null && !connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-closeConnector.CoordinateSystem.BasisZ))
				{
					XYZ xyz3 = closeConnector.CoordinateSystem.BasisZ.CrossProduct(-connector.CoordinateSystem.BasisZ).Normalize();
					double degree = 360.0 - YJKRevitTools.GetTwoVectorAngle(closeConnector.CoordinateSystem.BasisZ, -connector.CoordinateSystem.BasisZ, xyz3) * 180.0 / Math.PI;
					if (xyz3.IsAlmostEqualTo(new XYZ(0.0, 0.0, 0.0)))
					{
						xyz3 = new XYZ(0.0, 0.0, 1.0);
					}
					hyrotateFamilyInstance.RotateElem(familyInstance2, point, xyz3, degree);
					document.Regenerate();
				}
				Connector closeConnector3 = hypiping3.GetCloseConnector(point, false);
				Connector distanceConnector3 = hypiping3.GetDistanceConnector(point);
				int num = 0;
				XYZ vector = (closeConnector.Origin - distanceConnector2.Origin).Normalize();
				while (!this.IsSameConnector(closeConnector3, connector2, 0.008))
				{
					double angle = this.GetAngle(teeOutConnector.CoordinateSystem.BasisZ, connector2.CoordinateSystem.BasisZ, -1.0 * closeConnector3.CoordinateSystem.BasisZ);
					hyrotateFamilyInstance.RotateElem(familyInstance2, xyz, vector, angle * 180.0 / Math.PI);
					document.Regenerate();
					num++;
					if (num > 10)
					{
						break;
					}
				}
				connector = connectHelper.GetNearConnector(familyInstance2, distanceConnector);
				this.IsSameConnector(closeConnector, connector);
				this.IsSameConnector(closeConnector2, teeOutConnector);
				distanceConnector3 = hypiping3.GetDistanceConnector(point);
				connector2 = connectHelper.GetNearConnector(familyInstance2, distanceConnector3);
				this.IsSameConnector(closeConnector3, connector2);
				this.ConnConnectToPipe(hypiping, connector);
				this.ConnConnectToPipe(hypiping3, connector2);
				this.ConnConnectToPipe(hypiping4, teeOutConnector);
				document.Regenerate();
				ElementTransformUtils.MoveElement(hypiping3.Piping.Document, hypiping3.Piping.Id, new XYZ(1E-05, 1E-05, 1E-05));
				ElementTransformUtils.MoveElement(hypiping3.Piping.Document, hypiping3.Piping.Id, -1.0 * new XYZ(1E-05, 1E-05, 1E-05));
				FamilyInstance familyInstance3 = familyInstance2;
				subTransaction.Commit();
				result = familyInstance3;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (familyInstance != null)
				{
					document.DeleteElement(familyInstance);
				}
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		private void ConnConnectToPipe(YJKPiping<Pipe> myCustomPipe, Connector teeSideConnector)
		{
			if (YJKRevitTools.isRiserPipe(myCustomPipe.Piping))
			{
				if ((teeSideConnector.CoordinateSystem.BasisZ.Z > 0.0 && myCustomPipe.StartConnector.Connector.CoordinateSystem.BasisZ.Z < 0.0) || (teeSideConnector.CoordinateSystem.BasisZ.Z < 0.0 && myCustomPipe.StartConnector.Connector.CoordinateSystem.BasisZ.Z > 0.0))
				{
					myCustomPipe.StartConnector.Connector.ConnectTo(teeSideConnector);
					return;
				}
				if ((teeSideConnector.CoordinateSystem.BasisZ.Z > 0.0 && myCustomPipe.EndConnector.Connector.CoordinateSystem.BasisZ.Z < 0.0) || (teeSideConnector.CoordinateSystem.BasisZ.Z < 0.0 && myCustomPipe.EndConnector.Connector.CoordinateSystem.BasisZ.Z > 0.0))
				{
					myCustomPipe.EndConnector.Connector.ConnectTo(teeSideConnector);
					return;
				}
			}
			else
			{
				double num = teeSideConnector.CoordinateSystem.BasisZ.AngleTo(myCustomPipe.StartConnector.Connector.CoordinateSystem.BasisZ) * 180.0 / Math.PI;
				double num2 = teeSideConnector.CoordinateSystem.BasisZ.AngleTo(myCustomPipe.EndConnector.Connector.CoordinateSystem.BasisZ) * 180.0 / Math.PI;
				if (num > num2)
				{
					myCustomPipe.StartConnector.Connector.ConnectTo(teeSideConnector);
					return;
				}
				myCustomPipe.EndConnector.Connector.ConnectTo(teeSideConnector);
			}
		}

		private void SettingTeeParameter(FamilyInstance teeFi, YJKPiping<Pipe> hyPipe, YJKPiping<Pipe> hyPipeOther, int i)
		{
			IList<Connector> appropriateConnector = hyPipe.GetAppropriateConnector(hyPipeOther.Piping);
			double num = 0.0;
			if (appropriateConnector != null && appropriateConnector.Count > 1)
			{
				if (appropriateConnector[0].CoordinateSystem.BasisZ.IsAlmostEqualTo(-appropriateConnector[1].CoordinateSystem.BasisZ))
				{
					num = 0.0;
				}
				else
				{
					num = appropriateConnector[0].CoordinateSystem.BasisZ.AngleTo(-appropriateConnector[1].CoordinateSystem.BasisZ);
				}
			}
			Parameter parameter = hyPipeOther.Piping.get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM);
			if (1 == i)
			{
				if (parameter != null)
				{
					Parameter parameter2 = teeFi.GetParameter("出口直径");
					if (parameter2 != null && !parameter2.IsReadOnly)
					{
						parameter2.Set(parameter.AsDouble());
					}
				}
				if (num > 0.0001)
				{
					Parameter parameter3 = teeFi.GetParameter("出口角度");
					if (parameter3 != null && !parameter3.IsReadOnly)
					{
						parameter3.Set(num);
						return;
					}
					if (Math.Abs(num - Math.PI*.5) > 0.0001)
					{
						throw new Exception("管件没有出口角度或角度为只读，请选择正确的管件");
					}
				}
			}
			else if (2 == i)
			{
				if (parameter != null)
				{
					Parameter parameter4 = teeFi.GetParameter("支管直径");
					if (parameter4 != null && !parameter4.IsReadOnly)
					{
						parameter4.Set(parameter.AsDouble());
					}
				}
				if (num > 0.0001)
				{
					Parameter parameter5 = teeFi.GetParameter("角度");
					if (parameter5 == null || parameter5.AsDouble().ToString() == null)
					{
						parameter5 = teeFi.GetParameter("支管角度");
					}
					if (parameter5 != null && !parameter5.IsReadOnly)
					{
						parameter5.Set(num);
						return;
					}
					if (Math.Abs(num - Math.PI*.5) > 0.0001)
					{
						throw new Exception("管件没有支管角度或角度为只读，请选择正确的管件");
					}
				}
			}
		}

		private bool IsSameConnector(Connector conn1, Connector conn2)
		{
			bool result = true;
			if (conn1 == null || conn2 == null)
			{
				return false;
			}
			double num = conn1.CoordinateSystem.BasisZ.AngleTo(-conn2.CoordinateSystem.BasisZ);
			if (!conn1.CoordinateSystem.BasisZ.IsAlmostEqualTo(-conn2.CoordinateSystem.BasisZ) || num > 0.001)
			{
				result = false;
			}
			else if (Math.Abs(conn1.Radius - conn2.Radius) > 0.001)
			{
				result = false;
			}
			return result;
		}

		public List<Pipe> TPipeConnectRPipe(Pipe riserPipe, Pipe pipe, Autodesk.Revit.DB.Document doc, FamilySymbol fs, bool isSSTee, ref string errorMSG, bool isHengguan = false, double midPipeDiameter = 0.0)
		{
			Pipe pipe2;
			if (isHengguan)
			{
				pipe2 = pipe;
			}
			else
			{
				pipe2 = riserPipe;
			}
			double num;
			if (midPipeDiameter != 0.0)
			{
				num = midPipeDiameter / 304.8;
			}
			else
			{
				num = pipe2.get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble();
			}
			SubTransaction subTransaction = new SubTransaction(doc);
			List<Pipe> list = new List<Pipe>();
			Line line = (riserPipe.Location as LocationCurve).Curve as Line;
			Line line2 = (pipe.Location as LocationCurve).Curve as Line;
			XYZ hyendPoint = line.GetEndPoint(0);
			XYZ hyendPoint2 = line.GetEndPoint(1);
			XYZ hyendPoint3 = line2.GetEndPoint(0);
			XYZ hyendPoint4 = line2.GetEndPoint(1);
			double num2 = riserPipe.get_Parameter(BuiltInParameter.RBS_PIPE_OUTER_DIAMETER).AsDouble();
			double num3 = pipe.get_Parameter(BuiltInParameter.RBS_PIPE_OUTER_DIAMETER).AsDouble();
			YJKLine hyline = new YJKLine(line2);
			new YJKLine(line);
			XYZ xyz = hyline.IntersectionPoint(line, 0.001);
			double num4 = pipe.get_Parameter(BuiltInParameter.RBS_PIPE_SLOPE).AsDouble();
			double num5;
			if (num2 > num3)
			{
				num5 = 2.26 * num2;
			}
			else if (num2 == num3)
			{
				num5 = 2.65 * num2;
			}
			else if (num3 > 1.5 * num2 && num3 < 2.0 * num2)
			{
				num5 = 2.25 * num2 + 0.5 * num3;
			}
			else if (num3 > 2.0 * num2 && num3 < 3.0 * num2)
			{
				num5 = 2.7 * num2 + 0.5 * num3;
			}
			else
			{
				num5 = 3.5 * num2 + 0.5 * num3;
			}
			List<FamilyInstance> list2 = new List<FamilyInstance>();
			try
			{
				subTransaction.Start();
				if (YJKLine.judgeTwoLineRelativePosition(line, line2, num2, num3) == 1)
				{
					if (YJKLine.judgeTwoLineTheirAreInTerst(line, num2, line2, num3) == 2)
					{
						List<Pipe> list3 = new List<Pipe>();
						list3 = new YJKPiping<Pipe>(pipe).Separate(xyz);
						FamilyInstance item;
						if (!isSSTee)
						{
							item = doc.Create.NewTeeFitting(this.getCloserConnector(list3[0], xyz), this.getCloserConnector(list3[1], xyz), this.getCloserConnector(riserPipe, xyz));
						}
						else
						{
							item = this.NewTeeFittingY(this.newPipeListUseForTeeFitting(list3, riserPipe), fs, isSSTee);
						}
						list.AddRange(list3);
						list2.Add(item);
					}
					else if (YJKLine.judgeTwoLineTheirAreInTerst(line, num2, line2, num3) == 3 || YJKLine.judgeTwoLineTheirAreInTerst(line, num2, line2, num3) == 5 || YJKLine.judgeTwoLineTheirAreInTerst(line, num2, line2, num3) == 6)
					{
						this.changePipeElbow(pipe);
						this.changePipeElbow(riserPipe);
						FamilyInstance item2 = doc.Create.NewElbowFitting(this.getCloserConnector(pipe, xyz), this.getCloserConnector(riserPipe, xyz));
						list.Add(pipe);
						list2.Add(item2);
					}
					else if (YJKLine.judgeTwoLineTheirAreInTerst(line, num2, line2, num3) == 4)
					{
						List<Pipe> list4 = new List<Pipe>();
						list4 = new YJKPiping<Pipe>(riserPipe).SeparateHoldSelf(xyz);
						FamilyInstance item3;
						if (!isSSTee)
						{
							item3 = doc.Create.NewTeeFitting(this.getCloserConnector(list4[0], xyz), this.getCloserConnector(list4[1], xyz), this.getCloserConnector(pipe, xyz));
						}
						else
						{
							item3 = this.NewTeeFittingY(this.newPipeListUseForTeeFitting(list4, pipe), fs, isSSTee);
						}
						list.Add(pipe);
						list2.Add(item3);
					}
				}
				else if (YJKLine.judgeTwoLineRelativePosition(line, line2, num2, num3) == 2)
				{
					bool flag = false;
					XYZ dlineP = YJKRevitTools.GetDLineP(hyendPoint3, hyendPoint4, hyendPoint, hyendPoint2);
					XYZ dlineP2 = YJKRevitTools.GetDLineP(hyendPoint, hyendPoint2, hyendPoint3, hyendPoint4);
					Pipe pipe3 = YJKRevitTools.NewPipe(pipe2, dlineP, dlineP2);
					pipe3.get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(num);
					LocationCurve locationCurve = pipe3.Location as LocationCurve;
					XYZ hyendPoint5 = (pipe3.Location as LocationCurve).Curve.GetEndPoint(0);
					XYZ hyendPoint6 = (pipe3.Location as LocationCurve).Curve.GetEndPoint(1);
					double num6 = hyendPoint5.DistanceTo(hyendPoint6);
					if (num4 != 0.0 && !YJKRevitTools.isRiserPipe(pipe3))
					{
						double num7 = num6 * Math.Sin(num4);
						if (hyendPoint5.DistanceTo(dlineP) < hyendPoint6.DistanceTo(dlineP))
						{
							XYZ xyz2 = new XYZ(hyendPoint5.X, hyendPoint5.Y, hyendPoint5.Z - num7);
							YJKRevitTools.getNewPipe(pipe3, xyz2, doc);
							Line curve = YJKLineEx.YJKGetBound(xyz2, hyendPoint6);
							locationCurve.Curve = curve;
						}
						else
						{
							Line curve2 = YJKLineEx.YJKGetBound(new XYZ(hyendPoint6.X, hyendPoint6.Y, hyendPoint6.Z - num7), hyendPoint5);
							locationCurve.Curve = curve2;
						}
						ElementTransformUtils.MoveElement(pipe3.Document, pipe3.Id, new XYZ(0.001, 0.001, 0.001));
						ElementTransformUtils.MoveElement(pipe3.Document, pipe3.Id, -1.0 * new XYZ(0.001, 0.001, 0.001));
					}
					if (num6 >= num5)
					{
						List<Pipe> list5 = new List<Pipe>();
						list5 = new YJKPiping<Pipe>(riserPipe).Separate(dlineP);
						FamilyInstance item4;
						if (!isSSTee)
						{
							item4 = doc.Create.NewTeeFitting(this.getCloserConnector(list5[0], dlineP), this.getCloserConnector(list5[1], dlineP), this.getCloserConnector(pipe3, dlineP));
						}
						else
						{
							item4 = this.NewTeeFittingY(this.newPipeListUseForTeeFitting(list5, pipe3), fs, isSSTee);
						}
						this.changePipeElbow(pipe);
						this.changePipeElbow(pipe3);
						FamilyInstance item5 = doc.Create.NewElbowFitting(this.getCloserConnector(pipe3, dlineP2), this.getCloserConnector(pipe, dlineP2));
						list.Add(pipe);
						list2.Add(item4);
						list2.Add(item5);
					}
					else if (!flag)
					{
						doc.DeleteElement(pipe3);
						double num8 = Math.Sqrt(num5 * num5 - num6 * num6);
						XYZ xyz3 = (hyendPoint.DistanceTo(dlineP2) > hyendPoint4.DistanceTo(dlineP2)) ? hyendPoint3 : hyendPoint4;
						hyendPoint3.DistanceTo(dlineP2);
						hyendPoint4.DistanceTo(dlineP2);
						if (xyz3.DistanceTo(dlineP2) < num8)
						{
							errorMSG = "空间位置不足，无法创建过渡管道连接！";
						}
						else
						{
							XYZ xyz4 = dlineP2 + num8 * (xyz3 - dlineP2).Normalize();
							Pipe pipe4 = YJKRevitTools.NewPipe(riserPipe, dlineP, xyz4);
							List<Pipe> list6 = new List<Pipe>();
							list6 = new YJKPiping<Pipe>(riserPipe).Separate(dlineP);
							if (!isSSTee)
							{
								doc.Create.NewTeeFitting(this.getCloserConnector(list6[0], dlineP), this.getCloserConnector(list6[1], dlineP), this.getCloserConnector(pipe4, dlineP));
							}
							else
							{
								this.NewTeeFittingY(this.newPipeListUseForTeeFitting(list6, pipe4), fs, isSSTee);
							}
							doc.Regenerate();
							this.changePipeElbow(pipe4);
							this.changePipeElbow(pipe);
							Connector closerConnector = this.getCloserConnector(pipe4, xyz4);
							Connector closerConnector2 = this.getCloserConnector(pipe, xyz4);
							if (closerConnector.CoordinateSystem.BasisZ.AngleTo(closerConnector2.CoordinateSystem.BasisZ) * 180.0 / Math.PI < 175.0)
							{
								doc.Create.NewElbowFitting(closerConnector, closerConnector2);
							}
							else
							{
								LocationCurve locationCurve2 = pipe.Location as LocationCurve;
								XYZ xyz5 = xyz4 + 0.3 * num8 * (xyz3 - dlineP2).Normalize();
								Line curve3 = YJKLineEx.YJKGetBound(xyz3, xyz5);
								locationCurve2.Curve = curve3;
								closerConnector2 = this.getCloserConnector(pipe, xyz5);
								FlexPipeType flexPipeType = YJKElementFilter.GetElementByClass(typeof(FlexPipeType), doc).ElementAt(0) as FlexPipeType;
								doc.Create.NewFlexPipe(closerConnector, closerConnector2, flexPipeType).get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(riserPipe.get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble());
							}
							list.Add(pipe);
						}
						doc.Regenerate();
					}
				}
				else if (YJKLine.judgeTwoLineRelativePosition(line, line2, num2, num3) == 3)
				{
					XYZ dlineP3 = YJKRevitTools.GetDLineP(hyendPoint3, hyendPoint4, hyendPoint, hyendPoint2);
					XYZ dlineP4 = YJKRevitTools.GetDLineP(hyendPoint, hyendPoint2, hyendPoint3, hyendPoint4);
					Pipe pipe5 = YJKRevitTools.NewPipe(pipe2, dlineP3, dlineP4);
					pipe5.get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(num);
					XYZ hyendPoint7 = (pipe5.Location as LocationCurve).Curve.GetEndPoint(0);
					XYZ hyendPoint8 = (pipe5.Location as LocationCurve).Curve.GetEndPoint(1);
					LocationCurve locationCurve3 = pipe5.Location as LocationCurve;
					double num9 = hyendPoint7.DistanceTo(hyendPoint8);
					bool flag2 = false;
					if (num4 != 0.0 && !YJKRevitTools.isRiserPipe(pipe5))
					{
						double num10 = num9 * Math.Sin(num4);
						if (hyendPoint7.DistanceTo(dlineP3) < hyendPoint8.DistanceTo(dlineP3))
						{
							Line curve4 = YJKLineEx.YJKGetBound(new XYZ(hyendPoint7.X, hyendPoint7.Y, hyendPoint7.Z + num10), hyendPoint8);
							locationCurve3.Curve = curve4;
						}
						else
						{
							Line curve5 = YJKLineEx.YJKGetBound(new XYZ(hyendPoint8.X, hyendPoint8.Y, hyendPoint8.Z + num10), hyendPoint7);
							locationCurve3.Curve = curve5;
						}
						ElementTransformUtils.MoveElement(pipe5.Document, pipe5.Id, new XYZ(0.001, 0.001, 0.001));
						ElementTransformUtils.MoveElement(pipe5.Document, pipe5.Id, -1.0 * new XYZ(0.001, 0.001, 0.001));
					}
					if (num9 >= num5)
					{
						this.changePipeElbow(pipe);
						this.changePipeElbow(pipe5);
						this.changePipeElbow(riserPipe);
						FamilyInstance item6 = doc.Create.NewElbowFitting(this.getCloserConnector(pipe5, dlineP4), this.getCloserConnector(pipe, dlineP4));
						FamilyInstance item7 = doc.Create.NewElbowFitting(this.getCloserConnector(pipe5, dlineP3), this.getCloserConnector(riserPipe, dlineP3));
						list.Add(pipe);
						list2.Add(item6);
						list2.Add(item7);
					}
					else if (!flag2)
					{
						doc.DeleteElement(pipe5);
						double num11 = Math.Sqrt(num5 * num5 - num9 * num9);
						num11 = 1.1 * num11;
						XYZ xyz6 = (hyendPoint.DistanceTo(dlineP3) > hyendPoint2.DistanceTo(dlineP3)) ? hyendPoint : hyendPoint2;
						hyendPoint.DistanceTo(dlineP3);
						hyendPoint2.DistanceTo(dlineP3);
						if (xyz6.DistanceTo(dlineP3) < num11)
						{
							errorMSG = "空间位置不足，无法创建过渡管道连接！";
						}
						else
						{
							XYZ xyz7 = dlineP3 + num11 * (xyz6 - dlineP3).Normalize();
							Pipe pipe6 = YJKRevitTools.NewPipe(riserPipe, dlineP4, xyz7);
							this.changePipeElbow(pipe);
							this.changePipeElbow(pipe6);
							this.changePipeElbow(riserPipe);
							doc.Create.NewElbowFitting(this.getCloserConnector(pipe6, dlineP4), this.getCloserConnector(pipe, dlineP4));
							Connector closerConnector3 = this.getCloserConnector(pipe6, xyz7);
							Connector closerConnector4 = this.getCloserConnector(riserPipe, dlineP3);
							if (closerConnector3.CoordinateSystem.BasisZ.AngleTo(closerConnector4.CoordinateSystem.BasisZ) * 180.0 / Math.PI < 175.0)
							{
								doc.Create.NewElbowFitting(closerConnector3, closerConnector4);
							}
							else
							{
								LocationCurve locationCurve4 = riserPipe.Location as LocationCurve;
								XYZ xyz8 = xyz7 + 0.3 * num11 * (xyz6 - dlineP3).Normalize();
								Line curve6 = YJKLineEx.YJKGetBound(xyz6, xyz8);
								locationCurve4.Curve = curve6;
								closerConnector4 = this.getCloserConnector(riserPipe, xyz8);
								FlexPipeType flexPipeType2 = YJKElementFilter.GetElementByClass(typeof(FlexPipeType), doc).ElementAt(0) as FlexPipeType;
								doc.Create.NewFlexPipe(closerConnector3, closerConnector4, flexPipeType2).get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(riserPipe.get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble());
							}
							list.Add(pipe);
						}
						doc.Regenerate();
					}
				}
				else if (YJKLine.judgeTwoLineRelativePosition(line, line2, num2, num3) == 4)
				{
					XYZ dlineP5 = YJKRevitTools.GetDLineP(hyendPoint3, hyendPoint4, hyendPoint, hyendPoint2);
					XYZ dlineP6 = YJKRevitTools.GetDLineP(hyendPoint, hyendPoint2, hyendPoint3, hyendPoint4);
					Pipe pipe7 = YJKRevitTools.NewPipe(pipe2, dlineP5, dlineP6);
					pipe7.get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(num);
					XYZ hyendPoint9 = (pipe7.Location as LocationCurve).Curve.GetEndPoint(0);
					XYZ hyendPoint10 = (pipe7.Location as LocationCurve).Curve.GetEndPoint(1);
					LocationCurve locationCurve5 = pipe7.Location as LocationCurve;
					double num12 = hyendPoint9.DistanceTo(hyendPoint10);
					bool flag3 = false;
					if (num4 != 0.0 && !YJKRevitTools.isRiserPipe(pipe7))
					{
						double num13 = num12 * Math.Sin(num4);
						if (hyendPoint9.DistanceTo(dlineP5) < hyendPoint10.DistanceTo(dlineP5))
						{
							Line curve7 = YJKLineEx.YJKGetBound(new XYZ(hyendPoint9.X, hyendPoint9.Y, hyendPoint9.Z + num13), hyendPoint10);
							locationCurve5.Curve = curve7;
						}
						else
						{
							Line curve8 = YJKLineEx.YJKGetBound(new XYZ(hyendPoint10.X, hyendPoint10.Y, hyendPoint10.Z + num13), hyendPoint9);
							locationCurve5.Curve = curve8;
						}
						ElementTransformUtils.MoveElement(pipe7.Document, pipe7.Id, new XYZ(0.001, 0.001, 0.001));
						ElementTransformUtils.MoveElement(pipe7.Document, pipe7.Id, -1.0 * new XYZ(0.001, 0.001, 0.001));
					}
					if (num12 >= num5)
					{
						List<Pipe> list7 = new List<Pipe>();
						list7 = new YJKPiping<Pipe>(pipe).Separate(dlineP6);
						FamilyInstance item8;
						if (!isSSTee)
						{
							item8 = doc.Create.NewTeeFitting(this.getCloserConnector(list7[0], dlineP6), this.getCloserConnector(list7[1], dlineP6), this.getCloserConnector(pipe7, dlineP6));
						}
						else
						{
							item8 = this.NewTeeFittingY(this.newPipeListUseForTeeFitting(list7, pipe7), fs, isSSTee);
						}
						this.changePipeElbow(pipe7);
						this.changePipeElbow(riserPipe);
						FamilyInstance item9 = doc.Create.NewElbowFitting(this.getCloserConnector(pipe7, dlineP5), this.getCloserConnector(riserPipe, dlineP5));
						list.AddRange(list7);
						list2.Add(item8);
						list2.Add(item9);
					}
					else if (!flag3)
					{
						doc.DeleteElement(pipe7);
						double num14 = Math.Sqrt(num5 * num5 - num12 * num12);
						XYZ xyz9 = (hyendPoint.DistanceTo(dlineP5) > hyendPoint2.DistanceTo(dlineP5)) ? hyendPoint : hyendPoint2;
						hyendPoint.DistanceTo(dlineP5);
						hyendPoint2.DistanceTo(dlineP5);
						if (xyz9.DistanceTo(dlineP5) < num14)
						{
							errorMSG = "空间位置不足，无法创建过渡管道连接！";
						}
						else
						{
							XYZ xyz10 = dlineP5 + num14 * (xyz9 - dlineP5).Normalize();
							Pipe pipe8 = YJKRevitTools.NewPipe(riserPipe, dlineP6, xyz10);
							List<Pipe> list8 = new List<Pipe>();
							list8 = new YJKPiping<Pipe>(pipe).Separate(dlineP6);
							if (!isSSTee)
							{
								doc.Create.NewTeeFitting(this.getCloserConnector(list8[0], dlineP6), this.getCloserConnector(list8[1], dlineP6), this.getCloserConnector(pipe8, dlineP6));
							}
							else
							{
								this.NewTeeFittingY(this.newPipeListUseForTeeFitting(list8, pipe8), fs, isSSTee);
							}
							doc.Regenerate();
							this.changePipeElbow(pipe8);
							this.changePipeElbow(riserPipe);
							Connector closerConnector5 = this.getCloserConnector(pipe8, xyz10);
							Connector closerConnector6 = this.getCloserConnector(riserPipe, dlineP5);
							if (closerConnector5.CoordinateSystem.BasisZ.AngleTo(closerConnector6.CoordinateSystem.BasisZ) * 180.0 / Math.PI < 175.0)
							{
								doc.Create.NewElbowFitting(closerConnector5, closerConnector6);
							}
							else
							{
								LocationCurve locationCurve6 = riserPipe.Location as LocationCurve;
								XYZ xyz11 = xyz10 + 0.3 * num14 * (xyz9 - dlineP5).Normalize();
								Line curve9 = YJKLineEx.YJKGetBound(xyz9, xyz11);
								locationCurve6.Curve = curve9;
								closerConnector6 = this.getCloserConnector(riserPipe, xyz11);
								FlexPipeType flexPipeType3 = YJKElementFilter.GetElementByClass(typeof(FlexPipeType), doc).ElementAt(0) as FlexPipeType;
								doc.Create.NewFlexPipe(closerConnector5, closerConnector6, flexPipeType3).get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(riserPipe.get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble());
							}
							list.AddRange(list8);
						}
						doc.Regenerate();
					}
				}
				else if (YJKLine.judgeTwoLineRelativePosition(line, line2, num2, num3) == 5)
				{
					XYZ dlineP7 = YJKRevitTools.GetDLineP(hyendPoint3, hyendPoint4, hyendPoint, hyendPoint2);
					XYZ dlineP8 = YJKRevitTools.GetDLineP(hyendPoint, hyendPoint2, hyendPoint3, hyendPoint4);
					Pipe pipe9 = YJKRevitTools.NewPipe(pipe2, dlineP7, dlineP8);
					pipe9.get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(num);
					XYZ hyendPoint11 = (pipe9.Location as LocationCurve).Curve.GetEndPoint(0);
					XYZ hyendPoint12 = (pipe9.Location as LocationCurve).Curve.GetEndPoint(1);
					LocationCurve locationCurve7 = pipe9.Location as LocationCurve;
					double num15 = hyendPoint11.DistanceTo(hyendPoint12);
					bool flag4 = false;
					if (num4 != 0.0 && !YJKRevitTools.isRiserPipe(pipe9))
					{
						double num16 = num15 * Math.Sin(num4);
						if (hyendPoint11.DistanceTo(dlineP7) < hyendPoint12.DistanceTo(dlineP7))
						{
							Line curve10 = YJKLineEx.YJKGetBound(new XYZ(hyendPoint11.X, hyendPoint11.Y, hyendPoint11.Z - num16), hyendPoint12);
							locationCurve7.Curve = curve10;
						}
						else
						{
							Line curve11 = YJKLineEx.YJKGetBound(new XYZ(hyendPoint12.X, hyendPoint12.Y, hyendPoint12.Z - num16), hyendPoint11);
							locationCurve7.Curve = curve11;
						}
						ElementTransformUtils.MoveElement(pipe9.Document, pipe9.Id, new XYZ(0.001, 0.001, 0.001));
						ElementTransformUtils.MoveElement(pipe9.Document, pipe9.Id, -1.0 * new XYZ(0.001, 0.001, 0.001));
					}
					if (num15 >= num5)
					{
						List<Pipe> list9 = new List<Pipe>();
						list9 = new YJKPiping<Pipe>(pipe).Separate(dlineP8);
						FamilyInstance item10;
						if (!isSSTee)
						{
							item10 = doc.Create.NewTeeFitting(this.getCloserConnector(list9[0], dlineP8), this.getCloserConnector(list9[1], dlineP8), this.getCloserConnector(pipe9, dlineP8));
						}
						else
						{
							item10 = this.NewTeeFittingY(this.newPipeListUseForTeeFitting(list9, pipe9), fs, isSSTee);
						}
						List<Pipe> list10 = new List<Pipe>();
						list10 = new YJKPiping<Pipe>(riserPipe).Separate(dlineP7);
						FamilyInstance item11;
						if (!isSSTee)
						{
							item11 = doc.Create.NewTeeFitting(this.getCloserConnector(list10[0], dlineP7), this.getCloserConnector(list10[1], dlineP7), this.getCloserConnector(pipe9, dlineP7));
						}
						else
						{
							item11 = this.NewTeeFittingY(this.newPipeListUseForTeeFitting(list10, pipe9), fs, isSSTee);
						}
						list.AddRange(list9);
						list2.Add(item10);
						list2.Add(item11);
					}
					else if (!flag4)
					{
						doc.DeleteElement(pipe9);
						doc.Regenerate();
						errorMSG = "空间位置不足，无法创建过渡管道连接！";
					}
				}
				doc.Regenerate();
				using (List<Pipe>.Enumerator enumerator = list.GetEnumerator())
				{
					if (enumerator.MoveNext())
					{
						Pipe pipe10 = enumerator.Current;
						ElementTransformUtils.MoveElement(pipe10.Document, pipe10.Id, new XYZ(0.001, 0.001, 0.001));
						ElementTransformUtils.MoveElement(pipe10.Document, pipe10.Id, -1.0 * new XYZ(0.001, 0.001, 0.001));
					}
				}
				subTransaction.Commit();
			}
			catch
			{
				errorMSG = "连接失败！";
			}
			finally
			{
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return list;
		}

		public List<Pipe> TPipeConnectRPipe(Pipe riserPipe, XYZ xyzRUser, Pipe pipe, XYZ xyzHUser, Autodesk.Revit.DB.Document doc, ref string errorMSG, bool isHengguan = false)
		{
			Pipe sourcePipe;
			if (isHengguan)
			{
				sourcePipe = pipe;
			}
			else
			{
				sourcePipe = riserPipe;
			}
			SubTransaction subTransaction = new SubTransaction(doc);
			List<Pipe> list = new List<Pipe>();
			Line line = (riserPipe.Location as LocationCurve).Curve as Line;
			Line line2 = (pipe.Location as LocationCurve).Curve as Line;
			XYZ hyendPoint = line.GetEndPoint(0);
			XYZ hyendPoint2 = line.GetEndPoint(1);
			XYZ hyendPoint3 = line2.GetEndPoint(0);
			XYZ hyendPoint4 = line2.GetEndPoint(1);
			double num = riserPipe.get_Parameter(BuiltInParameter.RBS_PIPE_OUTER_DIAMETER).AsDouble();
			double num2 = pipe.get_Parameter(BuiltInParameter.RBS_PIPE_OUTER_DIAMETER).AsDouble();
			this.changePipeElbow(pipe);
			this.changePipeElbow(riserPipe);
			double num3 = pipe.get_Parameter(BuiltInParameter.RBS_PIPE_SLOPE).AsDouble();
			double num4;
			if (num > num2)
			{
				num4 = 2.26 * num;
			}
			else if (num == num2)
			{
				num4 = 2.65 * num;
			}
			else if (num2 > 1.5 * num && num2 < 2.0 * num)
			{
				num4 = 2.25 * num + 0.5 * num2;
			}
			else if (num2 > 2.0 * num && num2 < 3.0 * num)
			{
				num4 = 2.7 * num + 0.5 * num2;
			}
			else
			{
				num4 = 3.5 * num + 0.5 * num2;
			}
			List<FamilyInstance> list2 = new List<FamilyInstance>();
			try
			{
				subTransaction.Start();
				if (YJKLine.judgeTwoLineRelativePosition(line, line2, num, num2) == 1)
				{
					YJKLine hyline = new YJKLine(line2);
					YJKLine hyline2 = new YJKLine(line);
					XYZ xyz = hyline.IntersectionPoint(line, 0.001);
					Connector appropriateConnector = new YJKPiping<Pipe>(riserPipe, xyzRUser).GetAppropriateConnector(xyz);
					Connector appropriateConnector2 = new YJKPiping<Pipe>(pipe, xyzHUser).GetAppropriateConnector(xyz);
					XYZ xyz2 = hyline.GetProject(xyzHUser) - xyz;
					XYZ xyz3 = hyline2.GetProject(xyzRUser) - xyz;
					if (xyz2.AngleTo(xyz3) / Math.PI * 180.0 >= 89.999999998)
					{
						this.deleteConnectFitting(appropriateConnector);
						this.deleteConnectFitting(appropriateConnector2);
						FamilyInstance item = doc.Create.NewElbowFitting(appropriateConnector, appropriateConnector2);
						list.Add(pipe);
						list2.Add(item);
					}
					else
					{
						YJKMessageBox.Show("两管道夹角不应为锐角！");
					}
				}
				else
				{
					XYZ dlineP = YJKRevitTools.GetDLineP(hyendPoint3, hyendPoint4, hyendPoint, hyendPoint2);
					XYZ dlineP2 = YJKRevitTools.GetDLineP(hyendPoint, hyendPoint2, hyendPoint3, hyendPoint4);
					Pipe pipe2 = YJKRevitTools.NewPipe(sourcePipe, dlineP, dlineP2);
					XYZ hyendPoint5 = (pipe2.Location as LocationCurve).Curve.GetEndPoint(0);
					XYZ hyendPoint6 = (pipe2.Location as LocationCurve).Curve.GetEndPoint(1);
					LocationCurve locationCurve = pipe2.Location as LocationCurve;
					double num5 = hyendPoint5.DistanceTo(hyendPoint6);
					bool flag = false;
					if (num3 != 0.0 && !YJKRevitTools.isRiserPipe(pipe2))
					{
						double num6 = num5 * Math.Sin(num3);
						if (hyendPoint5.DistanceTo(dlineP) < hyendPoint6.DistanceTo(dlineP))
						{
							Line curve = YJKLineEx.YJKGetBound(new XYZ(hyendPoint5.X, hyendPoint5.Y, hyendPoint5.Z + num6), hyendPoint6);
							locationCurve.Curve = curve;
						}
						else
						{
							Line curve2 = YJKLineEx.YJKGetBound(new XYZ(hyendPoint6.X, hyendPoint6.Y, hyendPoint6.Z + num6), hyendPoint5);
							locationCurve.Curve = curve2;
						}
						ElementTransformUtils.MoveElement(pipe2.Document, pipe2.Id, new XYZ(0.001, 0.001, 0.001));
						ElementTransformUtils.MoveElement(pipe2.Document, pipe2.Id, -1.0 * new XYZ(0.001, 0.001, 0.001));
					}
					if (num5 >= num4)
					{
						this.changePipeElbow(pipe2);
						Connector appropriateConnector3 = new YJKPiping<Pipe>(riserPipe, xyzRUser).GetAppropriateConnector(dlineP);
						Connector appropriateConnector4 = new YJKPiping<Pipe>(pipe, xyzHUser).GetAppropriateConnector(dlineP2);
						FamilyInstance item2 = doc.Create.NewElbowFitting(this.getCloserConnector(pipe2, dlineP2), appropriateConnector4);
						FamilyInstance item3 = doc.Create.NewElbowFitting(this.getCloserConnector(pipe2, dlineP), appropriateConnector3);
						list.Add(pipe);
						list2.Add(item2);
						list2.Add(item3);
					}
					else if (!flag)
					{
						doc.DeleteElement(pipe2);
						double num7 = Math.Sqrt(num4 * num4 - num5 * num5);
						num7 = 1.1 * num7;
						Connector connector = null;
						Connector connector2 = null;
						YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(riserPipe, xyzRUser);
						if (hypiping.IsPointOnPipingCurve(dlineP))
						{
							connector = hypiping.GetAppropriateConnector(dlineP);
						}
						else
						{
							connector = hypiping.GetCloseConnector(dlineP, false);
						}
						YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipe, xyzHUser);
						if (hypiping2.IsPointOnPipingCurve(dlineP2))
						{
							connector2 = hypiping2.GetAppropriateConnector(dlineP2);
						}
						else
						{
							connector2 = hypiping2.GetCloseConnector(dlineP2, false);
						}
						Connector connector3 = null;
						foreach (Connector connector4 in YJKRevitTools.GetConnectorAll(riserPipe))
						{
							if (!connector4.Origin.IsAlmostEqualTo(connector.Origin))
							{
								connector3 = connector4;
								break;
							}
						}
						if (connector3.Origin.DistanceTo(dlineP) < num7)
						{
							errorMSG = "空间位置不足，无法创建过渡管道连接！";
						}
						else
						{
							XYZ xyz4 = dlineP + num7 * (connector3.Origin - dlineP).Normalize();
							Pipe pipe3 = YJKRevitTools.NewPipe(pipe, dlineP2, xyz4);
							doc.Regenerate();
							this.changePipeElbow(pipe3);
							doc.Create.NewElbowFitting(this.getCloserConnector(pipe3, dlineP2), connector2);
							doc.Create.NewElbowFitting(this.getCloserConnector(pipe3, xyz4), connector);
						}
						doc.Regenerate();
					}
					doc.Regenerate();
					using (List<Pipe>.Enumerator enumerator2 = list.GetEnumerator())
					{
						if (enumerator2.MoveNext())
						{
							Pipe pipe4 = enumerator2.Current;
							ElementTransformUtils.MoveElement(pipe4.Document, pipe4.Id, new XYZ(0.001, 0.001, 0.001));
							ElementTransformUtils.MoveElement(pipe4.Document, pipe4.Id, -1.0 * new XYZ(0.001, 0.001, 0.001));
						}
					}
				}
				subTransaction.Commit();
			}
			catch
			{
				errorMSG = "连接失败！";
			}
			finally
			{
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return list;
		}
	}
}
